// reading a text file
#include <stdio.h>
#include <conio.h>
#include <string.h>

void main() {
    FILE* file = fopen( "test.txt", "r" );
    if ( !file )
        exit(1);
        
    char buf[ 256 ];
    char prog_name[ 256 ];
	char prog_name_[ 256 ];
    char test_name[ 256 ];
    char prog_arg[ 256 ];
    char prog_control_res[ 256 ];
    char prog_res[ 256 ];
	char temp[256];
	char temp_[256];
    int  counter = 0;

    while ( fgets( buf, sizeof(buf), file ) ) {

		if ( counter == 0 ){
			printf( "%s\n", buf );
		} 
			
		if ( counter == 1 ){
			strcpy(test_name, buf);
			printf( "%s", test_name );
		} 

		 if ( counter == 2 ){
			 strcpy(prog_name, buf);
			 printf( "Start: %s", prog_name );
		 } 

		if ( counter == 3 ){
			strcpy(prog_control_res, buf);

			/* run programm and save result */

			char   psBuffer[128];
			FILE   *pPipe;

			/* Run DIR so that it writes its output to a pipe. Open this
			 * pipe with read text attribute so that we can read it 
			 * like a text file. 
			 */

		   if ( (pPipe = popen(prog_name, "rt" ) ) == NULL )
			  exit( 1 );
			
		   /* Read pipe until end of file, or an error occurs. */

		   int j = 1;
		   while(fgets(psBuffer, 128, pPipe))
		   {
			  printf("Answer line %d: %s", j, psBuffer);
			  j++;
			  strcpy(prog_res, psBuffer);
		   }

		   /* Close pipe and print return value of pPipe. */
		   if (feof( pPipe))
		   {
			 //printf( "\nProcess returned %d\n", _pclose( pPipe ) );
		   }
		   else
		   {
			 printf( "Error: Failed to read the pipe to the end.\n");
		   }

			counter = 0;
			
			//printf( "prog_control_res = %s", prog_control_res );
			//printf( "prog_res = %s", prog_res );
				
			if ( strcmp(prog_control_res, prog_res)  == 0 ){	
				printf( "OK\n\n" );
			}
			else{
				printf( "TEST DONT'N PASS!\n\n" );
			}
			
		}
		counter++;
    }
    
    fclose( file );
}